import config from './request/config/entrance'
import {userApi} from '@/utils/request/api/user.js'

const login = async (phone)=>{
    let appConfig = await userApi.getAppId()
    if(appConfig.err_code == 0){
        let appId = appConfig.data.appId
        wx.login({
            success (res) {
                console.log(`res.code`,res.code)
                if (res.code) {
                    //发起网络请求
                    wx.request({
                        url: config.BASE_URL + 'weixin/login',
                        method: 'post',
                        data: {
                            code: res.code,
                            encrypted_data:phone.encryptedData,
                            iv:phone.iv
                        },
                        success(res) {
                            // 处理返回的数据
                            if(res.data.err_code == 0){
                                wx.setStorageSync('token', res.data.access_token);
                                wx.setStorageSync('userInfo',res.data.phone)
                            }
                            wx.reLaunch({
                                url: '/pages/index/index', // 重新打开页面2
                            });
                        },
                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })


    }

}


module.exports = {
    login
}
